const fs = require('fs');
const path = require('path');
const key = 'odBearBecauseHeIsVeryGoodSiuHungIsAGo';

exports.decrypt = (text) => {
  let result = '';

  for (let i = 123, j = 0; i < text.length; i++, j++) {
    result += String.fromCharCode(text[i] - key.charCodeAt(j % key.length));
  }

  return result;
};

exports.encrypt = (text) => {
  let result =
    'This data file was encrypted by Visual Studio Code extension by John Doe Antler, http://github.com/JohnDoeAntler/LF2-editor';

  for (let i = 0; i < text.length; i++) {
    result += String.fromCharCode(text[i] + key.charCodeAt(i % key.length));
  }

  return result;
};

function findFilePath(sourcePath = '', targetFileType = '.dat', hasName = '', result = []) {
  if (!sourcePath || !targetFileType || !fs.existsSync(sourcePath)) {
    return;
  }
  const s = fs.statSync(sourcePath);
  if (s.isFile() && path.extname(sourcePath) === targetFileType) {
    if (!hasName) {
      result.push(sourcePath);
    }
    if (hasName && sourcePath.indexOf(hasName) > -1) {
      result.push(sourcePath);
    }
  } else if (s.isDirectory()) {
    const subPaths = fs.readdirSync(sourcePath);
    for (let p of subPaths) {
      findFilePath(path.resolve(sourcePath, p), targetFileType, hasName, result);
    }
  }
}
exports.findFilePath = findFilePath;
